<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Default Kohana controller. This controller should NOT be used in production.
 * It is for demonstration purposes only!
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class JQuerySlideTopMenu_Core{

	protected static $controller = '';
	protected static $action = '';
	protected static $segments = Router::segments;

	protected  function setupItems($items)
	{
		$ritems = array();

		foreach($items as $key =>$item)
		{
			//var_dump($item);
			if(isset($item['visible']) && !$item['visible'])
			continue;
			$item2=array();
			$item2['label']=$item['label'];
			if(is_array($item['url']))
			$item2['url']=  url::site($item['url'][0]);
			else
			$item2['url']=$item['url'];
			$pattern=isset($item['pattern'])?$item['pattern']:$item['url'];
			$item2['active']= self::isActive($pattern,Router::$controller, Router::$method);
			if(array_key_exists('subs',$item) && is_array($item['subs'])){
				$item2['subs'] = self::setupItems($item['subs']);
			}
			$ritems[]=$item2;
		}
		return $ritems;
	}
	protected  function isActive($pattern,$controllerID,$actionID)
	{
		if(!is_array($pattern) || !isset($pattern[0]))
		return false;

		if(strpos($pattern[0],'/')!==false)
		$matched=$pattern[0]===$controllerID.'/'.$actionID;
		else
		$matched=$pattern[0]===$controllerID;

		if($matched && count($pattern)>1)
		{
			foreach(array_splice($pattern,1) as $name=>$value)
			{
				if(!isset($_GET[$name]) || $_GET[$name]!=$value)
				return false;
			}
			return true;
		}
		else
		return $matched;
	}
	public static function buildMenu($menuid='slidemenu',$menuitem = null)
	{
		//var_dump($menuitem['items']);
		echo '<div id='.$menuid.' class="jqueryslidemenu"><ul>';

		$items = self::setupItems($menuitem['items']);
		//var_dump($items);
		echo self::JQuerySlideRenderMenu($items);

		echo '</ul><br style=\"clear: left\" /></div>';

		//echo '<script language= "javascript">jqueryslidemenu.buildmenu("slidemenu", arrowimages);</script>';
	}
	protected  function JQuerySlideRenderMenu($items)
	{
		foreach($items as $item){
			echo '<li>';
			echo html::anchor($item['url'],$item['label']);
			if(array_key_exists('subs',$item) && is_array($item['subs'])){
				echo "\n".'<ul>';
				self::JQuerySlideRenderMenu($item['subs']);
				echo '</ul>';
			}
			echo '</li>';
		}

	}

}